## Replication files for "Do Online Ads Influence Vote Choice?"
## anselm.rink@gmail.com
## This code replicates Table 1

rm(list = ls())
options(scipen=999)
set.seed(492960) #taken from random.org
library(foreign)
library(stargazer)
library(ri2)       
setwd("") ## set WD
data <- read.csv("data.csv") 

## define robust SE function 
cluster <- function(dat,fm, cluster){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm), 2 , function(x) tapply(x, cluster, sum, na.rm=T));
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  coeftest(fm, vcovCL) 
}


## run OLS models
model1 <- cluster(data, lm(cdu16erst ~ treated, data=data), data$PLZ)
model2 <- cluster(data, lm(cdu16zweit ~ treated, data=data), data$PLZ)
model3 <- cluster(data, lm(cdu16erst ~ treated + age6_15 + age15_18 + age18_27 + age27_45 + age45_55 + age55_65 + age_o65 + female + protestant + catholic + atheist_pop + foreign_pop + total_pop_ln + cdu11erst, data=data), data$PLZ)
model4 <- cluster(data, lm(cdu16zweit ~ treated + age6_15 + age15_18 + age18_27 + age27_45 + age45_55 + age55_65 + age_o65 + female + protestant + catholic + atheist_pop + foreign_pop + total_pop_ln + cdu11zweit, data=data), data$PLZ)


## print models
stargazer(model1, model2, model3, model4)




#### obtain RI p-values

## background declarations
N <- 100
declaration <- declare_ra(N = 189, m = 126)

## Model 1
Z <- data$treated
Y <- data$cdu16erst
dat <- data.frame(Y, Z)
ri_model1 <- conduct_ri(formula = Y ~ Z, declaration = declaration, assignment = "Z", sharp_hypothesis = 0, data = dat)
summary(ri_model1)[3]


## Model 2
Z <- data$treated
Y <- data$cdu16zweit
dat <- data.frame(Y, Z)
ri_model2 <- conduct_ri(formula = Y ~ Z, declaration = declaration, assignment = "Z", sharp_hypothesis = 0, data = dat)
summary(ri_model2)[3]


## Model 3
Z <- data$treated
Y <- data$cdu16erst
X1 <- data$age6_15 
X2 <- data$age15_18 
X3 <- data$age18_27 
X4 <- data$age27_45 
X5 <- data$age45_55 
X6 <- data$age55_65 
X7 <- data$age_o65 
X8 <- data$female 
X9 <- data$protestant 
X10 <- data$catholic 
X11 <- data$atheist_pop 
X12 <- data$foreign_pop 
X13 <- data$total_pop_ln 
X14 <- data$cdu11erst
dat <- data.frame(Y, Z, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14)
ri_model3 <- conduct_ri(formula = Y ~ Z + X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 + X9 + X10 + X11 + X12 + X13 + X14, declaration = declaration, assignment = "Z", sharp_hypothesis = 0, data = dat)
summary(ri_model3)[3]


## Model 4
Z <- data$treated
Y <- data$cdu16zweit
X1 <- data$age6_15 
X2 <- data$age15_18 
X3 <- data$age18_27 
X4 <- data$age27_45 
X5 <- data$age45_55 
X6 <- data$age55_65 
X7 <- data$age_o65 
X8 <- data$female 
X9 <- data$protestant 
X10 <- data$catholic 
X11 <- data$atheist_pop 
X12 <- data$foreign_pop 
X13 <- data$total_pop_ln 
X14 <- data$cdu11erst
dat <- data.frame(Y, Z, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14)
ri_model4 <- conduct_ri(formula = Y ~ Z + X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 + X9 + X10 + X11 + X12 + X13 + X14, declaration = declaration, assignment = "Z", sharp_hypothesis = 0, data = dat)
summary(ri_model4)[3]

